<?php
/**
 * Created by PhpStorm.
 * User:夏浩文
 * Date: 2017-06-29
 * Time: 上午 11:37
 */
namespace app\hongyanche\model;
use think\Db;
class Video {
    private $userId;
    private $auth;
    private $admin;
    private $config;
    function __construct()
    {
        //检查权限
        $this->admin = model('admin');
        $auth = $this->admin -> is('maintain');
        if($auth['state']){
            $this->auth = $auth['state'];
        }
        //获取user_id
        if (is_null($this->userId)) {
            $user_id = model('user');
            $userId = $this->userId = $user_id->getUserId();
        } else {
            $userId = $this->userId;
        }
        if (!$userId) {
            return;
        }

    }


    public function add($z)
    {
        if(!$this->userId) return ['state'=>0,'errormsg'=>'请登录'];
        header("Content-type:text/html;charset=utf-8");
        $upload = model('upload')->uploadFiles();
        if(!$upload['state']) return $upload;
        $str = md5(uniqid(microtime(true), true));
        $file = '/alidata/tp5/public/upload/' . $str . '.mp4';
        if($z) {
            exec("/usr/local/h264/ffmpeg-2.1.1/ffmpeg -i /alidata/tp5/public/$upload[data] -c:v libx264 -strict -2 $file", $a);
            if (!$a) {
                //删除源文件，减少占用空间
                unlink($upload['data']);
                //加入数据库
                $data['user_id'] = model('user')->getUserId();
                $data['videourl'] = 'http://' . $_SERVER['SERVER_NAME'] . '/upload/' . $str . '.mp4';
                $data['state'] = 1;
                $data['create_time'] = time();
                db('marketing_video')->insert($data);
                return ['state' => 1, 'data' => ['url' => $data['videourl']]];
            }else{
                return ['state'=>0,'errormsg'=>'转码失败'];
            }
        }else{
            $data['user_id'] = model('user')->getUserId();
            $data['videourl'] = 'http://' . $_SERVER['SERVER_NAME'] . '/' . $upload['data'];
            $data['state'] = 1;
            $data['create_time'] = time();
            db('marketing_video')->insert($data);
            return ['state' => 1, 'data' => ['url' => $data['videourl']]];
        }
    }
    function modify($id,$key,$modifyinfo){
        //id检查
        if (!($id > 0)) {
            return array('state' => 0, 'errormsg' => 'brand id incorrect', 'errorcode' => '');
        }
        $data[$key] = $modifyinfo;
        $brand_id = addslashes($id); //sql防注入
        $where = "video_id = '$brand_id'";
        $result = db('marketing_video')->where($where)->update($data);
        // 记录action表动作行为
        if ($result) {
            foreach ($data as $key => $value) {
                model('action')->add('video_modify', $key, $value);
            }
            $ret = array('state' => 1 ,'errormsg'=>'ok');
        } else {
            $ret = array('state' => 0, 'errormsg' => 'modify error');
        }
        return $ret;
    }
    function remove($id){
        if(!model('admin')->is('marketing')['state']){
            return ['state'=>0,'errormsg'=>'您没有权限'];
        }
        if(!$id) return ['state'=>0,'errormsg'=>'请选择一个视频'];
        $res = db('marketing_video')->where("video_id = $id")->update(['state'=>0]);
        if(!$res) return ['state'=>0,'errormsg'=>'删除失败'];
        return ['state'=>1,'errormsg'=>'删除成功'];
    }
}